package service.impl;

import java.sql.Timestamp;
import java.util.List;

import common.impl.exception.ServiceException;
import model.MQuiz;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import service.interf.IQuiz;
import dao.interf.IQuizDao;
import dao.interf.ITeacherDao;
import dao.interf.ITestDao;
import dao.pojo.Quiz;
import dao.pojo.Teacher;
import dao.pojo.Test;


public class QuizImpl implements IQuiz{
	private IQuizDao quizDao;
	private ITeacherDao teacherDao;
	private ITestDao testDao;

	public void setquizDao(IQuizDao quizDao) {
		this.quizDao = quizDao;
	}

	public void setTeacherDao(ITeacherDao teacherDao) {
		this.teacherDao = teacherDao;
	}

	public void setTestDao(ITestDao testDao) {
		this.testDao = testDao;
	}

	@Override
	public String add(MQuiz model) {
		Quiz quiz = new Quiz();

		Teacher tea = teacherDao.getById(model.getTeaId());
		if (tea == null) {
			throw new ServiceException("ERR-0021");
		} else {
			quiz.setTeacher(tea);
		}

		Test test = testDao.getById(model.getTestId());
		if (test == null) {
			throw new ServiceException("ERR-0051");
		} else {
			quiz.setTest(test);
		}
		
		quiz.setQuizName(model.getQuizName());
		quiz.setQuizClasses(model.getQuizClasses());
		quiz.setQuizDeadline(model.getQuizDeadline());
		quiz.setQuizFullScore(model.getQuizFullScore());
		quiz.setQuizPassScore(model.getQuizPassScore());

		quiz.setQuizCreateDate(new Timestamp(System.currentTimeMillis()));

		return quizDao.add(quiz);
	}

	@Override
	public String getById(MQuiz model) {
		Quiz quiz = quizDao.getById(model.getQuizId());
		if(null == quiz) {
			throw new ServiceException("ERR-0061");
		}
		return createQuizObj(quiz).toString();
	}

	@Override
	public String deleteById(Integer id) {
		quizDao.deleteById(id);
		return "1";
	}

	@Override
	public String edit(MQuiz model) {
		Quiz quiz = quizDao.getById(model.getQuizId());
		if (null == quiz) {
			throw new ServiceException("ERR-0061");
		}

		if(quiz.getTeacher().getTeaId() != model.getTeaId())
		{
			Teacher tea = teacherDao.getById(model.getTeaId());
			if (tea == null) {
				throw new ServiceException("ERR-0021");
			} else {
				quiz.setTeacher(tea);
			}
		}

		if(quiz.getTest().getTestId() != model.getTestId())
		{
			Test test = testDao.getById(model.getTestId());
			if (test == null) {
				throw new ServiceException("ERR-0051");
			} else {
				quiz.setTest(test);
			}
		}

		quiz.setQuizName(model.getQuizName());
		quiz.setQuizDeadline(model.getQuizDeadline());
		quiz.setQuizClasses(model.getQuizClasses());
		quiz.setQuizFullScore(model.getQuizFullScore());
		quiz.setQuizPassScore(model.getQuizPassScore());

		quizDao.edit(quiz);
		return "1";
	}

	@Override
	public String getAll(Integer teaId) {
		Teacher teacher = teacherDao.getById(teaId);
		if(null == teacher) {
			throw new ServiceException("ERR-0111");
		}
		List<Quiz> list = quizDao.getListByProperty("teacher.teaId", teacher.getTeaId().toString());
		
		return createQuizArr(list).toString();
	}

	private JSONObject createQuizObj(Quiz quiz) {
		JSONObject obj = new JSONObject();
		obj.put("quizId", quiz.getQuizId());
		if(null != quiz.getTeacher()) {
			obj.put("teaId", quiz.getTeacher().getTeaId());
			obj.put("teaName", quiz.getTeacher().getTeaName());
		}
		if(null != quiz.getTest())
			obj.put("testId", quiz.getTest().getTestId());
		obj.put("quizClasses", quiz.getQuizClasses());
		obj.put("quizName", quiz.getQuizName());
//		obj.put("quizCreateDate", DateHandle.timeGetDate(quiz.getQuizCreateDate()));
//		obj.put("quizDeadline", DateHandle.timeGetDate(quiz.getQuizDeadline()));
		obj.put("quizCreateDate", quiz.getQuizCreateDate().toString().subSequence(0, 19));
		obj.put("quizDeadline", quiz.getQuizDeadline().toString().subSequence(0, 19));
		obj.put("quizFullScore", quiz.getQuizFullScore());
		obj.put("quizPassScore", quiz.getQuizPassScore());

		Timestamp current = new Timestamp(System.currentTimeMillis());
		int compare = current.compareTo(quiz.getQuizDeadline());
		if (compare < 0) {
			obj.put("state", 0);
		} else {
			obj.put("state", 1);
		}
		
		return obj;
	}
	
	private JSONArray createQuizArr(List<Quiz> list) {
		JSONArray jarr = new JSONArray();
		if(null != list) {
			for (Quiz quiz : list) {
				jarr.add(createQuizObj(quiz));
			}
		}
		return jarr;
	}
}
